//
//  UserManger.m
//  ReceiveTask
//
//  Created by WangZhenyu on 2018/11/21.
//  Copyright © 2018 WangZhenyu. All rights reserved.
//

#import "UserManger.h"
#import "UserInfo.h"
#import "LocalRecordModel.h"
#import "FileManager.h"

static NSMutableDictionary *usersById = nil;

@implementation UserManger


+ (void)initDictionary{
    if (usersById == nil) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"USERINFO"];
        if (data != nil) {
            usersById = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        }
        if (usersById == nil) {
            usersById = [NSMutableDictionary dictionary];
        }
    }
}

+ (UserInfo*)getUserWithId:(NSString*)userId{
    [UserManger initDictionary];
    return [usersById objectForKey:userId];
}

+ (void)setUser:(UserInfo*)user{
    if (user.platformId == nil) {
        return;
    }
    [UserManger initDictionary];
    [usersById setObject:user forKey:user.platformId];
    [UserManger saveData];
}

+ (void)saveData{
    [UserManger initDictionary];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:usersById];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"USERINFO"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (UserInfo *)currentLoggedInUser{
    if (((NSString *)[DEFAULTS objectForKey:USERID]).length > 0) {
        return [UserManger getUserWithId:[DEFAULTS objectForKey:USERID]];
    }else{
        return nil;
    }
}

+ (BOOL)hasUserLogged {
    if (((NSString *)[DEFAULTS objectForKey:USERID]).length > 0) {
        if (![UserManger currentLoggedInUser]) {
            return NO;
        }
        if ([((NSString *)[DEFAULTS objectForKey:USERID]) isEqualToString:@"游客"]) {
            return NO;
        }
        return YES;
    }else{
        return NO;
    }
}

+ (void)logoutCurrentUser:(void (^)(void))success {
    NSLog(@"删除别名%@", [UserManger currentLoggedInUser].platformId);
    [DEFAULTS setObject:@"" forKey:USERID];
    [DEFAULTS synchronize];
}


+ (void)createFileModel:(LocalRecordModel *)model {
    model.isSelected = NO;
    NSMutableArray *files = [NSMutableArray array];
    UserInfo *user = [UserManger currentLoggedInUser];
    [files addObjectsFromArray:user.files];
    [files insertObject:model atIndex:0];
    user.files = files;
    [UserManger setUser:user];
}

+ (void)removeFileModel:(LocalRecordModel *)model {
    model.isDeleted = YES;
    model.isSelected = NO;
    NSMutableArray *files = [NSMutableArray array];
    UserInfo *user = [UserManger currentLoggedInUser];
    [files addObjectsFromArray:user.files];
    for (LocalRecordModel *obj in files) {
        if ([obj.recordId isEqualToString:model.recordId]) {
            [files removeObject:obj];
            break;
        }
    }
    user.files = files;
    [UserManger setUser:user];
    [[FileManager sharedManager] removeObject:[[FileManager sharedManager] getLocalFilePath:model.fileName]];
}

+ (void)saveFile {
    [UserManger setUser:[UserManger currentLoggedInUser]];
}
@end
